package com.asher.hmone;

import com.google.zxing.client.ohos.component.FinderComponent;
import com.asher.zxingmodule.BaseAbility;
import com.google.zxing.Result;
import com.google.zxing.client.ohos.camera.*;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.SurfaceOps;
import ohos.app.Context;
import ohos.eventhandler.InnerEvent;
import ohos.media.image.PixelMap;
import ohos.multimodalinput.event.KeyEvent;

import java.util.Optional;


public class ScanAbility extends BaseAbility implements SurfaceOps.Callback{

    private Context mContext= null;
    private SurfaceProvider surfaceProvider;
    private Text tResult;
    private FinderComponent finderComponent;
    private boolean success = false;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(com.asher.hmone.ResourceTable.Layout_scan_layout_ability);
        // 查找布局中组件
        mContext = this;
        cameraManager = new CameraManager(getApplicationContext(),this);
        initView();
    }

    private void initView() {
        tResult = (Text) findComponentById(com.asher.hmone.ResourceTable.Id_result);
        finderComponent = (FinderComponent) findComponentById(com.asher.hmone.ResourceTable.Id_finc);
        surfaceProvider = (SurfaceProvider) findComponentById(com.asher.hmone.ResourceTable.Id_surfaceProvider);
        Optional<SurfaceOps> ops = surfaceProvider.getSurfaceOps();
        ops.get().addCallback(this);
        tResult.setVisibility(Component.INVISIBLE);

    }

    @Override
    protected void onActive() {
        super.onActive();
        cameraManager.startLoopTrigger();
        finderComponent.setCameraManager(cameraManager);
        finderComponent.animStart();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        cameraManager.stopTrigger();
    }

    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackground() {
        super.onBackground();
    }

    @Override
    protected void onStop() {
        super.onStop();
        cameraManager.stop();
    }

    /*surface callback*/
    @Override
    public void surfaceCreated(SurfaceOps surfaceOps) {
        cameraManager.creatCamera(new CameraStateImpl(cameraManager,surfaceProvider.getSurfaceOps().get().getSurface(),getLayoutParams().width,getLayoutParams().height));
    }

    @Override
    public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceOps surfaceOps) {

    }
    /*surface callback*/

    @Override
    public void onDecodeSuccess(Result result) {
        super.onDecodeSuccess(result);
        success = true;
        tResult.setText(result.getText());
        PixelMap pixelMap = getSuccessPixelMap(result);
        tResult.setVisibility(Component.VISIBLE);
        cameraManager.stopTrigger();
        finderComponent.drawResultBitmap(pixelMap);
//        InnerEvent event = InnerEvent.get(ScanAbilityHandler.SHOURESULT);
//        mEventHandler.sendEvent(event,2000);
    }

    private PixelMap getSuccessPixelMap(Result result) {
        return resultMap;
    }

    public void triContinue(){
        success = false;
        tResult.setText("");
        tResult.setVisibility(Component.INVISIBLE);
        cameraManager.startLoopTrigger();
        finderComponent.drawViewfinder();

    }



    @Override
    public void onDecodeFail() {
        super.onDecodeFail();
        cameraManager.startCallbackHandler();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
        switch (keyCode) {
            case KeyEvent.KEY_BACK:
                if (success) {
                InnerEvent event = InnerEvent.get(ScanAbilityHandler.SHOURESULT);
                mEventHandler.sendEvent(event,2000);
                    return true;
                }
                break;
        }
        return super.onKeyDown(keyCode, keyEvent);
    }
}
